using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;


namespace SharePointExplorer
{
    /// <summary>
    /// Class call SharpZipLib library for create ZIP
    /// </summary>
    class ZIPLib
    {

        /// <summary>
        /// Add all file from the specified directory in ZipedFile
        /// </summary>
        /// <param name="DirectoryToZip">Directory to ZIP</param>
        /// <param name="ZipedFilePath">ZIP File</param>
        public static void ZipFolder(string DirectoryToZip, string ZipedFilePath)
        {
            DirectoryInfo di;
            FileInfo[] fis;
            byte[] tampon;
            FileStream fZip;
            FileStream fs;
            ZipOutputStream zipOStream;
            ZipEntry entry;
            try
            {
                di = new DirectoryInfo(DirectoryToZip);
                if (di.Exists)
                {
                    fis = di.GetFiles();
                    fZip = File.Create(ZipedFilePath);
                    zipOStream = new ZipOutputStream(fZip);
                    zipOStream.SetLevel(6);
                    zipOStream.SetComment("Files extracted from SharePoint Site (Date:" + DateTime.Now.ToString() + ")");
                    foreach (FileInfo fi in fis)
                    {
                        fs = File.OpenRead(fi.FullName);
                        tampon = new byte[fs.Length];
                        fs.Read(tampon, 0, tampon.Length);
                        entry = new ZipEntry((fi.Name));
                        zipOStream.PutNextEntry(entry);
                        zipOStream.Write(tampon, 0, tampon.Length);
                    }
                    zipOStream.Finish();
                    zipOStream.Close();
                    fZip.Close();
                }
            }
            catch
            { }
            finally
            {
                di = null;
                fis = null;
                tampon = null;
                fZip = null;
                fs = null;
                zipOStream = null;
                entry = null;
            }
        
        }

    }
}
